package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Shelf;
import com.ruoyi.system.service.IShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 货架Controller
 *
 * @author ruoyi
 * @date 2025-10-09
 */
@RestController
@RequestMapping("/system/shelf")
public class ShelfController extends BaseController {
    @Autowired
    private IShelfService shelfService;

    /**
     * 查询货架列表
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:list')")
    @GetMapping("/list")
    public TableDataInfo list(Shelf shelf,
                              @RequestParam(required = false) Long beginMaterialQuantity,
                              @RequestParam(required = false) Long endMaterialQuantity)
    {
        startPage();
        shelf.getParams().put("beginMaterialQuantity", beginMaterialQuantity);
        shelf.getParams().put("endMaterialQuantity", endMaterialQuantity);
        List<Shelf> list = shelfService.selectShelfList(shelf);
        return getDataTable(list);
    }

    /**
     * 导出货架列表
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:export')")
    @Log(title = "货架", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Shelf shelf) {
        List<Shelf> list = shelfService.selectShelfList(shelf);
        ExcelUtil<Shelf> util = new ExcelUtil<Shelf>(Shelf.class);
        util.exportExcel(response, list, "货架数据");
    }

    /**
     * 获取货架详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(shelfService.selectShelfById(id));
    }

    /**
     * 新增货架
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:add')")
    @Log(title = "货架", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody Shelf shelf) {
        return toAjax(shelfService.insertShelf(request, shelf));
    }

    /**
     * 修改货架
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:edit')")
    @Log(title = "货架", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Shelf shelf) {
        return toAjax(shelfService.updateShelf(shelf));
    }

    /**
     * 删除货架
     */
    @PreAuthorize("@ss.hasPermi('system:shelf:remove')")
    @Log(title = "货架", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(shelfService.deleteShelfByIds(ids));
    }
}
